#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Globalization;
using TVC.Layout;

#endregion

namespace TVC.Modules.Business
{
    public partial class InvoiceUpdate : Form
    {
        readonly string _strType = "INV_UPD";
        public InvoiceUpdate()
        {
            InitializeComponent();
        }

        public InvoiceUpdate(string strType, DataTable dt, MainForm form)
        {
            InitializeComponent();
            this._strType = strType;
            if (dt != null)
            {
                DataColumn dclSelect = new DataColumn("Select", Type.GetType("System.Boolean"));
                dclSelect.DefaultValue = false;
                if (!dt.Columns.Contains("Select"))
                    dt.Columns.Add(dclSelect);

                TVC.Layout.GridLayout layout = null;
                layout = new GridLayout(MainForm.ServerPath + "Resources\\Layout\\", String.Format("{0}Documents\\Layout\\{1}\\", MainForm.ServerPath, form.PODInfo.USER_ID), _strType);
                layout.SetUpDGVWithCheck(dgvList);

                dgvList.DataSource = dt;
                dt = null;
            }
            LoadComboBox();
            MainForm = form;
        }

        private void LoadComboBox()
        {
            DataTable dtb = new DataTable();
            dtb.Columns.Add("Name");
            dtb.Columns.Add("Value");

            dtb.Rows.Add("Department", "Department");
            dtb.Rows.Add("Invoice No.", "InvNo");
            dtb.Rows.Add("Our Ref.", "OurRef");
            dtb.Rows.Add("Remark 1", "Remark1");
            dtb.Rows.Add("Remark 2", "Remark2");
            dtb.Rows.Add("Remark 3", "Remark3");
            dtb.Rows.Add("Returned Date", "ReturnedDate");
            dtb.Rows.Add("Vendor Code", "VendorCode");
            dtb.Rows.Add("Delivery Date", "DeliveryDate");
            cboField.DataSource = dtb;
            cboField.DisplayMember = "Name";
            cboField.ValueMember = "Value";
        }
        public MainForm MainForm { get; set; }

        private void BatchUpdate_Load(object sender, EventArgs e)
        {

        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string sErr = "";
            string name = "";
            object value = null;
            value = txtValue.Text;
            if (cboField.SelectedValue != null && cboField.SelectedValue.ToString() != "")
                name = cboField.SelectedValue.ToString();

            try
            {
                if (name == "ReturnedDate" || name == "DeliveryDate")
                {
                    DateTime dt;
                    var x = txtValue.Text;
                    if (DateTime.TryParseExact(x, "d/M/y", new CultureInfo("en-US"), DateTimeStyles.None, out dt))
                    {
                        value = dt;
                    }
                    else if (DateTime.TryParseExact(x, "d-M-y", new CultureInfo("en-US"), DateTimeStyles.None, out dt))
                    {
                        value = dt;
                    }
                }
            }
            catch (Exception ex) { sErr = ex.Message; MessageBox.Show("Invalide Format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, false); return; }

            BUS.InvoiceRegisterControl ctr = new BUS.InvoiceRegisterControl();
            foreach (DataGridViewRow r in dgvList.Rows)
            {
                if (r.Cells["colSelect"].Value != null && (bool)r.Cells["colSelect"].Value == true)
                {
                    DTO.InvoiceRegisterInfo inf = new DTO.InvoiceRegisterInfo((DataRowView)r.DataBoundItem);
                    if (name == "VendorCode")
                    {
                        BUS.VENDORControl ctrVD = new BUS.VENDORControl();
                        if (ctrVD.IsExist(MainForm.PODInfo.Business_ID, value.ToString()))
                        {
                            sErr = ctr.UpdateField(MainForm.PODInfo.Business_ID, name, value, inf);
                            if (string.IsNullOrEmpty(sErr))
                            {
                                var value1 = ctrVD.Get_VENDOR(value.ToString(), ref sErr);
                                if (value1 != null) sErr = ctr.UpdateField(MainForm.PODInfo.Business_ID, "Creditor", value1.VendorName, inf);
                            }
                        }
                        else sErr = string.Format("Vendor {0} DO NOT exist", value.ToString());
                    }
                    else sErr = ctr.UpdateField(MainForm.PODInfo.Business_ID, name, value, inf);

                }
            }


            if (sErr != "") MessageBox.Show(sErr, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, false);
            if (sErr == "")
            {
                MessageBox.Show("Your record is successfull updated!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information, false);
                DialogResult = Gizmox.WebGUI.Forms.DialogResult.OK;
                Close();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cboField_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtValue.Text = "";

        }

        private void txtValue_TextChanged(object sender, EventArgs e)
        {
            //if (cboField.SelectedValue != null && cboField.SelectedValue.ToString() == "ReturnedDate")
            //{
            //    var x = cboField.SelectedValue.ToString();
            //    if (x.Contains("/"))
            //    {
            //        DateTime dt;
            //        if (DateTime.TryParseExact(x, "dd/MM/yyyy", new CultureInfo("en-US"), DateTimeStyles.None, out dt))
            //        {
            //            txtValue.Text = dt.ToString("yyyy-MM-dd");
            //        }
            //    }
            //}
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MarkAll();
        }
        private void MarkAll()
        {
            bool mark = button1.Text == "Mark All";

            foreach (DataGridViewRow r in dgvList.Rows)
            {
                r.Cells["colSelect"].Value = mark;
            }
            if (mark) button1.Text = "Unmark All";
            else button1.Text = "Mark All";
        }
    }
}